package com.sds.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sds.dao.BusDAO;
import com.sds.frontcontroller.FrontController_Interface;
import com.sds.util.Util_Servlet;
import com.sds.vo.BusVO;

public class BusController implements FrontController_Interface {
	private BusDAO BusDAO = new BusDAO();

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		System.out.println("[ Location : BusController ]");

		if("insertForm".equals(request.getParameter("type"))) {
			Util_Servlet.forward(request, response, "jsp_bus/insertBusForm.jsp");
		} else if("updateForm".equals(request.getParameter("type"))) {
			BusVO BusVO = BusDAO.selectBus(request.getParameter("bus_id"));

			if(BusVO != null) {				
				request.setAttribute("BUS", BusVO);
				
				Util_Servlet.forward(request, response, "jsp_bus/updateBusForm.jsp");
			}
		} else if("insert".equals(request.getParameter("type"))) {
			String bus_id = request.getParameter("BUS_ID");
			String bus_company = request.getParameter("BUS_COMPANY");
						
			BusVO BusVO = BusDAO.selectBus(bus_id);

			if(BusVO != null) {
				Util_Servlet.sendRedirect(response, "jsp_bus/insertBusForm.jsp");
			} else {
				if(bus_id != null && bus_company != null) {
					if(bus_id.length() != 0 && bus_company.length() != 0) {
						BusDAO.insertBus(new BusVO(bus_id, bus_company));
						
						ArrayList<BusVO> busList = BusDAO.selectBusList();
						
						request.setAttribute("BUS_LIST", busList);
						
						Util_Servlet.forward(request, response, "jsp_bus/selectBusListForm.jsp");
					} else {
						Util_Servlet.sendRedirect(response, "jsp_bus/insertBusForm.jsp");
					}
				} else {
					Util_Servlet.sendRedirect(response, "jsp_bus/insertBusForm.jsp");
				}
			}			
		} else if("select".equals(request.getParameter("type"))) {
			BusVO BusVO = BusDAO.selectBus(request.getParameter("bus_id"));

			if(BusVO != null) {
				request.setAttribute("BUS", BusVO);

				Util_Servlet.forward(request, response, "jsp_bus/selectBusForm.jsp");
			}
		} else if("selectList".equals(request.getParameter("type"))) {		
			ArrayList<BusVO> BusVOList = BusDAO.selectBusList();
			
			request.setAttribute("BUS_LIST", BusVOList);

			Util_Servlet.forward(request, response, "jsp_bus/selectBusListForm.jsp");
		} else if("update".equals(request.getParameter("type"))) {
			String bus_id = request.getParameter("BUS_ID");
			String bus_company = request.getParameter("BUS_COMPANY");
			
			BusVO BusVO = BusDAO.selectBus(bus_id);
			
			if(BusVO != null) {
				if(bus_id != null && bus_company != null) {
					if(bus_id.length() != 0 && bus_company.length() != 0) {
						BusDAO.updateBus(new BusVO(bus_id, bus_company));
						
						ArrayList<BusVO> busList = BusDAO.selectBusList();
						
						request.setAttribute("BUS_LIST", busList);
						
						Util_Servlet.forward(request, response, "jsp_bus/selectBusListForm.jsp");
					} else {
						Util_Servlet.sendRedirect(response, "jsp_bus/updateBusForm.jsp");
					}
				} else {
					Util_Servlet.sendRedirect(response, "jsp_bus/updateBusForm.jsp");
				}
			} else {
				Util_Servlet.sendRedirect(response, "jsp_bus/updateBusForm.jsp");
			}			
		} else if("delete".equals(request.getParameter("type"))) {
			BusDAO.deleteBus(request.getParameter("bus_id"));
			
			ArrayList<BusVO> BusVOList = BusDAO.selectBusList();

			request.setAttribute("BUS_LIST", BusVOList);

			Util_Servlet.forward(request, response, "jsp_bus/selectBusListForm.jsp");
		}
	}
}